/**
 * 
 */
package com.pxingg.fep.serial;

import java.util.Vector;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Assert;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

import com.pxingg.log.LogConfig;

/**
 * @author pxingg
 * 
 */
public class SerialTest
{
    Serial serial;

    /**
     * The port connect to test
     */
    String testPort = "COM4";

    /**
     * The speed in the test
     */
    int testSpeed = 9600;
    /**
     * The log of this class
     */
    static Log log = LogFactory.getLog(SerialTest.class);

    /**
     * @throws java.lang.Exception
     */
    @BeforeClass
    public static void setUpBeforeClass() throws Exception
    {
        // 打开日志开关
        LogConfig.init();
    }

    /**
     * @throws java.lang.Exception
     */
    @AfterClass
    public static void tearDownAfterClass() throws Exception
    {
    }

    /**
     * @throws java.lang.Exception
     */
    @Before
    public void setUp() throws Exception
    {
        serial = new Serial("Arduino", "COM4");
    }

    /**
     * @throws java.lang.Exception
     */
    @After
    public void tearDown() throws Exception
    {
        serial.disconnect();
    }

    /**
     * Test method for {@link com.pxingg.fep.serial.Serial#connect(java.lang.String)}.
     */
    @Test
    public final void testConnectString()
    {
        serial.connect();
        Assert.assertTrue(serial.isConnected());
    }

    /**
     * Test method for {@link com.pxingg.fep.serial.Serial#connect(java.lang.String, int)} .
     */
    @Test
    public final void testConnectStringInt()
    {
        serial.connect();
        Assert.assertTrue(serial.isConnected());
        serial.disconnect();
        Assert.assertFalse(serial.isConnected());
    }

    /**
     * Test method for {@link com.pxingg.fep.serial.Serial#getPortList()}.
     */
    @Test
    public final void testGetPortList()
    {
        final Vector<String> ports = Serial.getPortList();

        System.out.print("the port in system are: ");
        for (final String port : ports)
        {
            System.out.print(port);
        }
        System.out.println();
        Assert.assertTrue(ports.size() > 0);
    }

    /**
     * Test method for send and receive of the Serial .
     */
    @Test
    public final void testProcess()
    {
        serial.connect();
        // ByteArrayQueue send = serial.getSendBuffer();
        // ByteArrayQueue receive = serial.getReceiveBuffer();
        //
        // Timer timer = new Timer();
        // timer.start();
        //
        // int sendDataLength = 60000;
        // int receiveDataLenth = 0;
        //
        // for (int i = 0; i < sendDataLength; i++)
        // {
        // if (send.currentCapacity() > 0)
        // {
        // send.append((byte) i);
        // }
        //
        // if (receive.size() > 0)
        // {
        // byte[] data = receive.serve(receive.size());
        // receiveDataLenth += data.length;
        // }
        // }
        //
        // while (timer.getInterval() < 300 * 1000)
        // {
        // if (receive.size() > 0)
        // {
        // byte[] data = receive.serve(receive.size());
        // receiveDataLenth += data.length;
        //
        // if (receiveDataLenth == sendDataLength)
        // {
        // break;
        // }
        // }
        //
        // try
        // {
        // Thread.sleep(50);
        // }
        // catch (InterruptedException e)
        // {
        // e.printStackTrace();
        // }
        // }
        //
        // timer.stop();
        // log.debug(timer);
        //
        // assertEquals(receiveDataLenth, sendDataLength);

    }

    /**
     * Test method for {@link com.pxingg.fep.serial.Serial#Serial(java.lang.String)}.
     */
    @Test
    public final void testSerial()
    {
        Assert.assertFalse(-1 == serial.toString().indexOf("Arduino"));
    }
}
